package com.wg.net.client.tcp;

import com.wg.net.client.WgClient;
import com.wg.net.client.core.AbstractClient;
import com.wg.net.client.core.ClientConfig;
import com.wg.net.codec.MyDecoder;
import com.wg.net.codec.MyEncoder;
import io.netty.channel.*;
import io.netty.channel.socket.SocketChannel;

/**
 * TCP 协议客户端实现
 *
 * @author 少爷123
 */
public class TcpClient extends AbstractClient {
    public TcpClient(WgClient wgClient, ClientConfig config) {
        super(wgClient, config);
    }

    @Override
    protected ChannelFuture doSend(byte[] data) {
        return channel.writeAndFlush(data);
    }

    @Override
    protected ChannelInitializer<? extends Channel> createInitializer() {
        return new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) {
                ch.pipeline().addLast(new MyDecoder());
                ch.pipeline().addLast(new MyEncoder());
                ch.pipeline().addLast(new TcpClientHandler(TcpClient.this));
            }
        };
    }
}
